package com.mugu.ipclient;

public class RefreshCmd {

	private final static byte REFRESH_CMD_REFRESH_ONLINE = 0x1e;
	private final static byte REFRESH_CMD_REFRESH_ONLINE_REPLY = 0x1f;
	private final static byte REFRESH_CMD_REFRESH_0A = 0x0a;
	private final static byte REFRESH_CMD_REFRESH_0A_REPLY = 0x0b;
	private final static byte REFRESH_CMD_REFRESH_GET_NEWS = 0x05;
	private final static byte REFRESH_CMD_REFRESH_GET_NEWS_REPLY = 0x06;

	private final static int REFRESH_PACKET_LENGTH = 500;
	private final static int REFRESH_PACKET_FILL_BYTE = 0xff;
	private final static int REFRESH_PACKET_RANDOM_STRING_LENGTH = 0x29;

	private final static String COPYRIGHT_VERSION = "@Version: 1.85, GUET";

	private String userName = "";
	private int refreshKey = 0;

	public RefreshCmd(String userName, int refreshKey) {
		this.userName = userName;
		this.refreshKey = refreshKey + 1500;
	}

	public void setRefrshKey(int refreshKey) {
		this.refreshKey = refreshKey + 1500;
	}

	private static byte[] packRefreshPacket(RefreshPacketModel model) {
		byte[] re = null;
		try {
			re = new byte[REFRESH_PACKET_LENGTH];
			int index = 0;
			for (byte b : IPClientUtils.short2byte(model.getBanner())) {
				re[index++] = b;
			}
			re[index++] = model.getCommand();
			for (byte b : IPClientUtils.long2byte(model.getKey())) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.long2byte((long) model.getFlow())) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.long2byte((long) model.getMoney())) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.int2byte(model.getLen1())) {
				re[index++] = b;
			}

			for (byte b : model.getStr1().getBytes("gbk")) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.int2byte(model.getLen2())) {
				re[index++] = b;
			}
			for (byte b : model.getStr2().getBytes("gbk")) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.int2byte(model.getLen3())) {
				re[index++] = b;
			}
			for (byte b : model.getMsg().getBytes("gbk")) {
				re[index++] = b;
			}
			for (byte b : IPClientUtils.int2byte(model.getLen4())) {
				re[index++] = b;
			}
			for (byte b : model.getStr4().getBytes("gbk")) {
				re[index++] = b;
			}
			for (int i = index; i < re.length; i++) {
				re[i] = (byte) REFRESH_PACKET_FILL_BYTE;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return re;
	}

	private static RefreshPacketModel unpackRefreshPacket(byte[] packet) {
		RefreshPacketModel model = null;
		try {
			if (packet != null && ((int) packet[0] & 0xff) == 0x82
					&& ((int) packet[1] & 0xff) == 0x23) {
				model = new RefreshPacketModel();

				model.setCommand(packet[2]);
				int index = 3;

				model.setKey(IPClientUtils.byte2long(packet, index));
				index += 8;

				model.setFlow(IPClientUtils.byte2double(packet, index));
				index += 8;

				model.setMoney(IPClientUtils.byte2double(packet, index));
				index += 8;

				model.setLen1(IPClientUtils.byte2int(packet, index));
				index += 4;
				model.setStr1(new String(packet, index, model.getLen1(), "gbk"));
				index += model.getLen1();

				model.setLen2(IPClientUtils.byte2int(packet, index));
				index += 4;
				model.setStr2(new String(packet, index, model.getLen2(), "gbk"));
				index += model.getLen2();

				model.setLen3(IPClientUtils.byte2int(packet, index));
				index += 4;
				model.setMsg(new String(packet, index, model.getLen3(), "gbk"));
				index += model.getLen3();

				model.setLen4(IPClientUtils.byte2int(packet, index));
				index += 4;
				model.setStr4(new String(packet, index, model.getLen4(), "gbk"));
				index += model.getLen4();

			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return model;
	}

	public static byte[] get_refresh_get_news_packet() {
		return packRefreshPacket(new RefreshPacketModel(
				REFRESH_CMD_REFRESH_GET_NEWS,//
				IPClientUtils.getRandomInt(888888, 111111),//
				IPClientUtils.getRandomInt(888888, 111111) << 32,//
				IPClientUtils.getRandomInt(888888, 111111) << 32,//
				"a", //
				"b", //
				IPClientUtils
				.getRandomString(REFRESH_PACKET_RANDOM_STRING_LENGTH), //
				COPYRIGHT_VERSION));
	}

	public static RefreshPacketModel check_refresh_get_news_reply(byte[] packet) {
		RefreshPacketModel model = unpackRefreshPacket(packet);
		if (model != null
				&& model.getCommand() == REFRESH_CMD_REFRESH_GET_NEWS_REPLY) {
			return model;
		}
		return null;
	}

	public byte[] get_refresh_0a_packet() {
		return packRefreshPacket(new RefreshPacketModel(REFRESH_CMD_REFRESH_0A,//
				IPClientUtils.getRandomInt(888888, 111111),//
				IPClientUtils.getRandomInt(888888, 111111) << 32,//
				IPClientUtils.getRandomInt(888888, 111111) << 32,//
				"a", //
				"b", //
				IPClientUtils
				.getRandomString(REFRESH_PACKET_RANDOM_STRING_LENGTH), //
				COPYRIGHT_VERSION));
	}

	public boolean check_refresh_0a_reply(byte[] packet) {
		RefreshPacketModel model = unpackRefreshPacket(packet);
		if (model != null && model.getCommand() == REFRESH_CMD_REFRESH_0A_REPLY) {
			return true;
		} else {
			return false;
		}
	}

	public byte[] get_refresh_online_packet() {
		return packRefreshPacket(new RefreshPacketModel(
				REFRESH_CMD_REFRESH_ONLINE,//
				refreshKey,//
				(long) IPClientUtils.getRandomInt(888888, 111111) << 32,//
				(long) IPClientUtils.getRandomInt(888888, 111111) << 32,//
				userName, //
				"b", //
				"今天天气真好", //
				COPYRIGHT_VERSION));
	}

	public RefreshPacketModel check_refresh_online_reply(byte[] packet) {
		RefreshPacketModel model = unpackRefreshPacket(packet);
		if (model != null
				&& model.getCommand() == REFRESH_CMD_REFRESH_ONLINE_REPLY) {
			return model;
		}
		return null;
	}

}
